//
//  ShopMallClassView.m
//  YBLive
//
//  Created by ybRRR on 2020/8/19.
//  Copyright © 2020 cat. All rights reserved.
//

#import "ShopMallClassView.h"

@implementation ShopMallClassView

-(instancetype)initWithFrame:(CGRect)frame andData:(NSArray *)dataArr{
    self = [super initWithFrame:frame];
    if (self) {
        listArr = dataArr;
        
        self.backgroundColor = UIColor.whiteColor;
        backScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 0, self.width-20, self.height)];
        backScroll.showsHorizontalScrollIndicator = NO;
        backScroll.backgroundColor = UIColor.whiteColor;
        backScroll.delegate = self;
        backScroll.layer.cornerRadius = 5;
        backScroll.layer.masksToBounds = YES;
        [self addSubview:backScroll];
        
        [self addBottomSlider];
        
        if (dataArr.count > 12) {
            backScroll.contentSize = CGSizeMake(((_window_width-20)/5.5)*(dataArr.count)/2+20, self.height);
        }else{
            backScroll.contentSize = CGSizeMake(((_window_width-20)/5.5)*(dataArr.count)+20, self.height);
        }
        if (dataArr.count > 12) {
            for (int i = 0 ; i <dataArr.count; i ++) {
                NSString *gc_iconStr =[dataArr[i]valueForKey:@"gc_icon"];

                UIButton *btn = [UIButton buttonWithType:0];
                btn.frame = CGRectMake(i/2*((_window_width-20)/5.5), i%2*55 +i%2*10, (_window_width-20)/5.5, 55);
                [btn addTarget:self action:@selector(classBtnClick:) forControlEvents:UIControlEventTouchUpInside];
                btn.tag = 10000+i;
                [backScroll addSubview:btn];
                UIImageView *img = [[UIImageView alloc]init];
                [img sd_setImageWithURL:[NSURL URLWithString:gc_iconStr]];
                [btn addSubview:img];
                [img mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.center.equalTo(btn);
                    make.height.width.mas_equalTo(35);
                    
                }];
                [btn addSubview:img];

                UILabel *titleLb = [[UILabel alloc]init];
                titleLb.font = [UIFont systemFontOfSize:11];
                titleLb.textColor = UIColor.blackColor;
                titleLb.textAlignment = NSTextAlignmentCenter;
                titleLb.text = minstr([dataArr[i]valueForKey:@"gc_name"]);
                [btn addSubview:titleLb];
                [titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(img);
                    make.top.equalTo(img.mas_bottom).offset(5);
                }];
            }
        }else{
            for (int i = 0; i < dataArr.count; i ++) {
                NSString *gc_iconStr =[dataArr[i]valueForKey:@"gc_icon"];

                UIButton *btn = [UIButton buttonWithType:0];
                btn.frame = CGRectMake(i * ((_window_width-20)/5.5), 0, ((_window_width-20)/5.5), 55);
                [btn addTarget:self action:@selector(classBtnClick:) forControlEvents:UIControlEventTouchUpInside];
                btn.tag = 10000+i;

                [backScroll addSubview:btn];
                UIImageView *img = [[UIImageView alloc]init];
                [img sd_setImageWithURL:[NSURL URLWithString:gc_iconStr]];
                [btn addSubview:img];
                [img mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.center.equalTo(btn);
                    make.height.width.mas_equalTo(35);
                    
                }];
                [btn addSubview:img];

                UILabel *titleLb = [[UILabel alloc]init];
                titleLb.font = [UIFont systemFontOfSize:11];
                titleLb.textColor = [UIColor blackColor];
                titleLb.textAlignment = NSTextAlignmentCenter;
                titleLb.text = minstr([dataArr[i]valueForKey:@"gc_name"]);
                [btn addSubview:titleLb];
                [titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(img);
                    make.top.equalTo(img.mas_bottom).offset(2);
                }];

            }
        }

    }
    return self;
}
-(void)addBottomSlider{
    slideBackView=[[UIView alloc] initWithFrame:CGRectMake(_window_width/2-20, backScroll.bottom-6, 40, 4)];
    [self addSubview:slideBackView];
     slideBackView.backgroundColor = [UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:1.00];
     slideBackView.layer.cornerRadius = 2;
    
    sliderView = [[UIView alloc] init];
    [slideBackView addSubview:sliderView];
    sliderView.frame=CGRectMake(0, 0, 15 ,4);
    sliderView.backgroundColor =Pink_Cor;
    sliderView.layer.cornerRadius = 2;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [UIView animateWithDuration:0.2 animations:^{
        
        CGPoint offset =  scrollView.contentOffset;
        // scrollView的当前位移/scrollView的总位移=滑块的当前位移/滑块的总位移
        //offset/(scrollView.contentSize.width-scrollView.frame.size.width)=滑块的位移/(slideBackView.frame.size.width-sliderView.frame.size.width)
        //        滑块距离屏幕左边的距离加上滑块的当前位移，即为滑块当前的x
        CGRect frame=sliderView.frame;
        frame.origin.x=1 +offset.x*(slideBackView.frame.size.width-sliderView.frame.size.width)/(scrollView.contentSize.width-scrollView.frame.size.width);
        
        sliderView.frame = frame;
    }];
}
-(void)classBtnClick:(UIButton *)sender{
    NSInteger index = sender.tag -10000;
    NSDictionary *dataDic = listArr[index];
    if ([self.delegate respondsToSelector:@selector(selectClassData:)]) {
        [self.delegate selectClassData:dataDic];
    }
}
@end
